#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->label_ig->setText(QString::number(ignoreH));

    btnInfo.choice=-1;
    btnInfo.dx=0;

    ignoreW=-1;


    resizeUi.init(this->width(),this->height());
    resizeUi.addInfo(ui->label);
    resizeUi.addInfo(ui->label2);
    resizeUi.addInfo(ui->btnH);
    resizeUi.addInfo(ui->btn1);
    resizeUi.addInfo(ui->btn2);
    resizeUi.addInfo(ui->btn3);
    resizeUi.addInfo(ui->btn4);
    resizeUi.addInfo(ui->btn5);
    resizeUi.addInfo(ui->btn6);
    resizeUi.addInfo(ui->btn7);

    resizeUi.addInfo(ui->btnW);
    resizeUi.addInfo(ui->pushButton_3);
    resizeUi.addInfo(ui->label_ig);

    ui->btn7->setEnabled(false);
    ui->pushButton_3->setEnabled(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::resizeEvent(QResizeEvent *event)
{
    resizeUi.resizes(this->width(),this->height());

    showImage(image1_show,1);
    showImage(image2_show,2);
}

void MainWindow::showImage(QImage img, int label)
{
    double scale=std::max(1.0*img.width()/ui->label->width(),1.0*img.height()/ui->label->height());
    QPixmap temp=QPixmap::fromImage(img);
    temp = temp.scaled(img.width()/scale,img.height()/scale);
    if(label==1)
        ui->label->setPixmap(temp);
    else
        ui->label2->setPixmap(temp);
}


int MainWindow::findCombinePlace()
{
    if(image1.width()!=image2.width())
    {
        ui->label_ig->setText(tr(" width is not equal!"));
        return -1;
    }

    int end_h = getEndPlace(ignoreH);

    image1_show=QImage(image1);
    for(int i=0;i<image1_show.width();i++)
    {
        for(int j=end_h;j<image1.height();j++)
            image1_show.setPixel(i,j,qRgb(200,180,0));
    }
    showImage(image1_show,1);

    return end_h;
}

int MainWindow::getEndPlace(int startH)
{
    int height1=image1.height();

    int detectH=overlap,c=0,j=0;
    if(height1<detectH)
        return height1;

    for(j=height1-detectH;j>=0;j--)
    {
        for(c=0;c<detectH;c++)
        {
            if(!isSameLine(image1,image2,j+c,c+startH))
                break;
        }
        if(c==detectH)
        {
            break;
        }
    }

    if(j<0)
        return height1;
    else
        return j;

}


bool MainWindow::isSameLine(QImage img1, QImage img2, int h1, int h2)
{
//    int max_dis=60;
    int w=img1.width();
    if(ignoreW!=-1)
        w=ignoreW;
    for(int i=0;i<w;i++)
    {
        QRgb c1=img1.pixel(i,h1);
        QRgb c2=img2.pixel(i,h2);

        int r1=qRed(c1),g1=qGreen(c1),b1=qBlue(c1);
        int r2=qRed(c2),g2=qGreen(c2),b2=qBlue(c2);

        if(fabs(r1-r2)>max_dis||fabs(g1-g2)>max_dis||fabs(b1-b2)>max_dis)
        {
            return false;
        }
    }
    return true;
}


void MainWindow::on_pushButton_3_clicked()
{
    int endh=findCombinePlace();

    QImage temp(image1.width(),endh+image2.height()-ignoreH+1,image1.format());

    int i=0,j=0;
    for(i=0;i<image1.width();i++)
    {
        for(j=0;j<endh;j++)
        {
            temp.setPixel(i,j,image1.pixel(i,j));
        }
    }

    for(i=0;i<image1.width();i++)
    {
        for(j=ignoreH;j<image2.height();j++)
        {
            temp.setPixel(i,endh+j-ignoreH,image2.pixel(i,j));
        }
    }

    image1=QImage(temp);
    image1_show=QImage(temp);
    showImage(image1_show,1);

    image1.save("temp.png","png");

    ui->btn7->setEnabled(true);
}

void MainWindow::on_btn7_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this, tr("Open Image"), "", tr("Image (*.png)"));
    if (!fileName.isNull())
    {
        image1.save(fileName,"png");
    }
}


void MainWindow::on_btnH_clicked()
{
    int dh=ui->btnH->pos().y()+ui->btnH->height()-ui->label2->pos().y();
    if(dh<0||dh>ui->label2->height())
        ignoreH=0;
    else{

        double scale=std::max(1.0*image2.width()/ui->label2->width(),1.0*image2.height()/ui->label2->height());
        ignoreH=dh*scale;

        image2_show=QImage(image2);
        for(int i=0;i<image2_show.width();i++)
        {
            for(int j=0;j<ignoreH;j++)
                image2_show.setPixel(i,j,qRgb(255,180,0));
        }
        for(int i=0;i<image2_show.height();i++)
        {
            int w=image2.width();
            if(ignoreW!=-1)
                w=ignoreW;
            for(int j=w;j<image2.width();j++)
                image2_show.setPixel(j,i,qRgb(0,180,255));
        }
        showImage(image2_show,2);

        ui->label_ig->setText(QString::number(ignoreH));
    }

    findCombinePlace();
}

void MainWindow::on_btnW_clicked()
{
    int dw=ui->btnW->pos().x()-ui->label2->pos().x();
    if(dw<0||dw>ui->label2->height())
        ignoreW=-1;
    else{

        double scale=std::max(1.0*image2.width()/ui->label2->width(),1.0*image2.height()/ui->label2->height());
        ignoreW=dw*scale;

        image2_show=QImage(image2);
        for(int i=0;i<image2_show.width();i++)
        {
            for(int j=0;j<ignoreH;j++)
                image2_show.setPixel(i,j,qRgb(255,180,0));
        }
        for(int i=0;i<image2_show.height();i++)
        {
            int w=image2.width();
            if(ignoreW!=-1)
                w=ignoreW;
            for(int j=w;j<image2.width();j++)
                image2_show.setPixel(j,i,qRgb(0,180,255));
        }
        showImage(image2_show,2);

        ui->label_ig->setText(QString::number(ignoreW));
    }

    findCombinePlace();
}


void MainWindow::on_btn1_clicked()
{
    QString filename;
    filename = QFileDialog::getOpenFileName(this,"Choose Src File","","(*.*)");
    if(filename == "")
        return;
    else
    {
        image1=QImage(filename);
        if(image1.width()>10)
        {
            image1_show=QImage(image1);
            showImage(image1_show,1);
            ui->btn1->setText(filename);

            if(image2.width()>10)
                ui->pushButton_3->setEnabled(true);
        }
        else
            ui->btn1->setText(tr("invalid file!"));
    }



}

void MainWindow::on_btn2_clicked()
{
    QString filename;
    filename = QFileDialog::getOpenFileName(this,"Choose Src File","","(*.*)");
    if(filename == "")
        return;
    else
    {
        image2=QImage(filename);
        if(image2.width()>10)
        {
            image2_show=QImage(image2);
            showImage(image2_show,2);
            ui->btn2->setText(filename);

            if(image1.width()>10)
                ui->pushButton_3->setEnabled(true);
        }
        else
            ui->btn2->setText(tr("invalid file!"));
    }
}

void MainWindow::on_btn3_clicked()
{
    max_dis++;
    ui->label_ig->setText(tr("max distance:")+QString::number(max_dis));

    findCombinePlace();
}

void MainWindow::on_btn4_clicked()
{
    max_dis--;
    if(max_dis<0)
        max_dis=0;
    ui->label_ig->setText(tr("max distance:")+QString::number(max_dis));

    findCombinePlace();
}

void MainWindow::on_btn6_clicked()
{
    overlap++;
    ui->label_ig->setText(tr("Overlap:")+QString::number(overlap));

    findCombinePlace();
}

void MainWindow::on_btn5_clicked()
{
    overlap--;
    if(overlap<0)
        overlap=0;
    ui->label_ig->setText(tr("Overlap:")+QString::number(overlap));

    findCombinePlace();
}






void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::RightButton)
    {
        int x=event->pos().x();
        int y=event->pos().y();

        int dx=x-ui->btnH->pos().x();
        int dy=y-ui->btnH->pos().y();
        if(dx>0&&dx<ui->btnH->width()&&dy>0&&dy<ui->btnH->height())
        {
            btnInfo.choice=1;
            btnInfo.dx=dx;
            btnInfo.dy=dy;
            return ;
        }

        dx=x-ui->btnW->pos().x();
        dy=y-ui->btnW->pos().y();
        if(dx>0&&dx<ui->btnW->width()&&dy>0&&dy<ui->btnW->height())
        {
            btnInfo.choice=2;
            btnInfo.dx=dx;
            btnInfo.dy=dy;
            return ;
        }

    }
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::RightButton)
    {
        if(btnInfo.choice==1){
            int w=ui->btnH->width();
            int h=ui->btnH->height();
            int x=event->pos().x();
            int y=event->pos().y();
            ui->btnH->setGeometry(x-btnInfo.dx,y-btnInfo.dy,w,h);
        }
        else if(btnInfo.choice==2){
            int w=ui->btnW->width();
            int h=ui->btnW->height();
            int x=event->pos().x();
            int y=event->pos().y();
            ui->btnW->setGeometry(x-btnInfo.dx,y-btnInfo.dy,w,h);
        }
    }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::RightButton)
    {
        btnInfo.choice=-1;
    }
}

